﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using UI.Lib.Factory;

namespace UI.Lib
{
    public class PageCache
    {
        /// <summary>
        /// 获取Sys5a00中的信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="ctl"> </param>
        /// <returns></returns>
        public static CommonObject GetSys5A00(string page, string ctl)
        {
            string key = string.Concat(page, "$$", ctl, "$", "Sys5a00");

            CommonObject cacheObj = Cache.Instance.Get<CommonObject>(key);

            if (null == cacheObj)
            {
                cacheObj = new CommonObject();
                cacheObj.SerializeObject(CommonHelper.Entity.GetSys5a00(page, ctl));
            }

            Cache.Instance.AddOrUpdate(key, cacheObj);

            return cacheObj;
        }

        public static void AddLayoutCache(string page, string ctl, DataTable columns)
        {
            string key = string.Concat(page, "$$", ctl);

            Cache.Instance.AddOrUpdate(key, columns);
        }

        private static DataTable ExistsLayout(string page, string ctl)
        {
            string key = string.Concat(page, "$$", ctl);

            return Cache.Instance.Get<DataTable>(key);
        }

        public static DataTable GetLayout(string page, string ctl)
        {
            var table = ExistsLayout(page, ctl);
            if (null == table)
            {
                table = CommonHelper.Entity.GetColumnByPageAndCtl(page, ctl);
            }

            string key = string.Concat(page, "$$", ctl);

            Cache.Instance.AddOrUpdate(key, table);

            return table;
        }

        public static Dictionary<string, DataTable> GetLayouts(string page, params string[] ctls)
        {
            List<string> needToLoad = new List<string>();
            Dictionary<string, DataTable> layouts = new Dictionary<string, DataTable>(); ;
            foreach (var ctl in ctls)
            {
                var table = ExistsLayout(page, ctl);

                if (table == null)
                {
                    needToLoad.Add(ctl);
                }
                else
                {
                    if (layouts.ContainsKey(ctl))
                    {
                        layouts[ctl] = table;
                    }
                    else
                    {
                        layouts.Add(ctl, table);
                    }
                }
            }

            if (needToLoad.Count > 0)
            {
                Dictionary<string, DataTable> ret = CommonHelper.Entity.GetColumnByPageAndCtls(
                                    page,
                                    needToLoad.ToArray()
                                    );

                foreach (var dict in ret)
                {
                    if (layouts.ContainsKey(dict.Key))
                    {
                        layouts[dict.Key] = dict.Value;
                    }
                    else
                    {
                        layouts.Add(dict.Key, dict.Value);
                    }
                }
            }

            return layouts;
        }
    }
}
